﻿function Location(province, district, commune, options) {
    this.province_ = $(province);
    this.district_ = $(district);
    this.commune_ = $(commune);
    this.options_ = options || {};

    // Ensure default
    var me = this;
    var ensureDefault = function (settingName, defaultValue) {
        me.options_[settingName] = (me.options_[settingName] == undefined) ? defaultValue : me.options_[settingName];
    };
    ensureDefault('soap', new Soap());
    ensureDefault('onProvinceChange', null);
    ensureDefault('provinceChanged', null);
    ensureDefault('onDistrictChange', null);
    ensureDefault('districtedChanged', null);
    ensureDefault('onCommuneChange', null);
    ensureDefault('communeChanged', null);
    delete ensureDefault;

    // Init
    var initialize = function () {
        me.province_.change(function () {
            if (me.options_['onProvinceChange']) {
                var onchange = me.options_['onProvinceChange'];
                // raise onchange
                onchange();
            }
            var soap = me.options_['soap'];
            soap.send('GetDistricts', { 'provinceCode': me.province_.find('option:selected').val() }, function (collection) {
                bindLocationCollection(me.district_, me.commune_, collection, 'provinceCode', me.options_['provinceChanged']);
            });
        });

        $(me.district_).change(function () {
            if (me.options_['onDistrictChange']) {
                var onchange = me.options_['onDistrictChange'];
                // raise onchange
                onchange();
            }
            var soap = me.options_['soap'];
            soap.send('GetCommunes', { 'provinceCode': me.province_.find('option:selected').val(), 'districtCode': me.district_.find('option:selected').val() }, function (collection) {
                bindLocationCollection(me.commune_, null, collection, 'districtCode', me.options_['districtChanged']);
            });
        });

        $(me.commune_).change(function () {
            if (me.options_['onCommuneChange']) {
                var onchange = me.options_['onCommuneChange'];
                // raise onchange
                onchange();
            }

            // Do sth

            if (me.options_['communeChanged']) {
                var changed = me.options_['communeChanged'];
                changed();
            }
        });
    };
    // Initializtion
    initialize();

    var bindLocationCollection = function (jLocation, jNextLocation, collection, locationIDName, callback) {
        if (jNextLocation) {
            jNextLocation.find('option').remove();
            jNextLocation.append('<option value="-1">- Tùy chọn -</option>'); ;
        }

        var $location = jLocation;
        // clear old collection
        $location.find('option').remove();
        // Insert default selection
        $location
                .append('<option value="-1">- Tùy chọn -</option>');

        if (collection instanceof jQuery) {
            collection.find('locationCollection location').each(function () {
                $location
                        .append('<option value="' + $(this).find(locationIDName).text() + '">' + $(this).find('name').text() + '</option>');
            });
        }

        if (callback) {
            callback();
        }
    };

    // CleanUp
    delete initialize;
}

Location.prototype.trigger = function () {
    var me = this;
    //me.province_.change();
};
